//
//  GropPictureView.m
//  GroupPicture_Dome
//
//  Created by 郑想 on 16/6/16.
//  Copyright © 2016年 YW. All rights reserved.
//
#import "YYPhotoGroupView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "GropPictureView.h"
#import "YWPatrolPicture.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
#import "YWPicture.h"
#import "Masonry.h"
#import "YWResourceManager.h"

#define KBaseGropViewTag 200

@interface GropPictureView()
@end

@implementation GropPictureView

- (void)initWithGroupItems:(NSArray *)groupItems withConstraint:(MASConstraint *) heightConstraint{
    self.clipsToBounds = YES;
    self.hidden = NO;
    if (self.subviews) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    _imagePices = @[].mutableCopy;
    CGFloat imageHigt = (SCREEN_WIDTH - 70)/3;
    NSUInteger count = groupItems.count;
    _items = [NSMutableArray array];
    
    YWPicture* picture = nil;
    YWPatrolPicture *patrolPicture = nil;
    NSString *pictureName = nil;
    NSString *smallPicUrl = nil;
    NSString *bigPicUrl = nil;
    BOOL isLoaclPic = NO;
    for (NSInteger i = 0; i < count; i++) {
        if (i>5) {
            break;
        }
        id pic = groupItems[i];
        if ([pic isKindOfClass:[YWPicture class]]) {
            picture = (YWPicture*)pic;
            isLoaclPic = picture.isLocalImage;
            pictureName = picture.pictureName;
            smallPicUrl = picture.smallPictureUrl;
            bigPicUrl = picture.bigPictureUrl;
        }else if ([pic isKindOfClass:[YWPatrolPicture class]]){
            patrolPicture = (YWPatrolPicture*)pic;
            isLoaclPic = patrolPicture.isLocalImage;
            pictureName = patrolPicture.pictureName;
            smallPicUrl = patrolPicture.smallUrl;
            bigPicUrl = patrolPicture.bigUrl;
        }
        YYPhotoGroupItem *item = [YYPhotoGroupItem new];
        
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.tag = KBaseGropViewTag + i;
        if ([groupItems[i] isKindOfClass:[UIImage class]]) {
            imageView.image = groupItems[i];
        } else {
            if (isLoaclPic) {
                NSData* imageData = [[YWResourceManager shareManager] dataFromCacheStorageWithResourcePath:pictureName];
                UIImage* image = [UIImage imageWithData:imageData];
                if (image) {
                    imageView.image = image;
                }
                else {
                    NSString *downloadUrl = [YWCommonTool pictureDownloadUrl:smallPicUrl];
                    NSURL *picUrl = [NSURL URLWithString:downloadUrl];
                    item.largeImageURL = [NSURL URLWithString:[YWCommonTool pictureDownloadUrl:bigPicUrl]];
                    [imageView sd_setImageWithURL:picUrl placeholderImage:[UIImage imageNamed:@"image_placeholder"]];
                }
            }else{
                NSString *downloadUrl = [YWCommonTool pictureDownloadUrl:smallPicUrl];
                NSURL *picUrl = [NSURL URLWithString:downloadUrl];
                item.largeImageURL = [NSURL URLWithString:[YWCommonTool pictureDownloadUrl:bigPicUrl]];
                [imageView sd_setImageWithURL:picUrl placeholderImage:[UIImage imageNamed:@"image_placeholder"]];
            }
        }
//        添加手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [imageView addGestureRecognizer:tapGesture];
        imageView.userInteractionEnabled = YES;
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.frame = CGRectMake(i%3 * (5 + imageHigt), i/3 * (imageHigt + 5) , imageHigt, imageHigt);
        [self addSubview:imageView];
        [_imagePices addObject:imageView];
        [_items addObject:item];
    }
    if (count == 0) {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(0)).priorityHigh();
        }];
    } else if(count > 3){
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(imageHigt*2 + 10)).priorityHigh();
        }];
    } else {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(imageHigt + 5)).priorityHigh();
        }];
    }
}

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    
    NSInteger tag = recognizer.view.tag - KBaseGropViewTag;
    NSMutableArray *items = [NSMutableArray new];
    for (NSUInteger i = 0, max = _imagePices.count; i < max; i++) {
        YYPhotoGroupItem *item = [YYPhotoGroupItem new];
        item.thumbView = _imagePices[i];
        item.largeImageURL = _items[i].largeImageURL;
        [items addObject:item];
    }
    UIView *fromView = _imagePices[tag];
    
    YYPhotoGroupView *v = [[YYPhotoGroupView alloc] initWithGroupItems:items];
    [v presentFromImageView:fromView toContainer:[UIApplication sharedApplication].keyWindow animated:YES completion:nil];
}


@end
